package com.util;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;

/**
 * @author: Combo
 * @time: 2020/12/27 17:59
 * @description: 工具类
 */
public class GameUtil {
    private GameUtil() {
    }

    /**
     * 根据地址获取图片
     * @param path 图片地址
     * @return Image
     */
    public static Image getImage(String path){
        BufferedImage bi = null;
        URL u = GameUtil.class.getClassLoader().getResource(path);
        Objects.requireNonNull(u, "图片不存在或路径错误！");
        try {
            bi = ImageIO.read(u);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bi;
    }

    /**
     * 根据地址返回图标图片
     * @param path 图片地址
     * @return ImageIcon
     */
    public static ImageIcon getIcon(String path){
        ImageIcon bi = null;
        URL u = GameUtil.class.getClassLoader().getResource(path);
        Objects.requireNonNull(u, "图片不存在或路径错误！");
        try {
            bi = new ImageIcon(ImageIO.read(u));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bi;
    }
}
